package com.qpx.jxcauth.service.impl;

import com.qpx.jxcauth.entity.TUser;
import com.qpx.jxcauth.mapper.TUserMapper;
import com.qpx.jxcauth.service.ITUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Mht
 * @since 2020-05-20
 *
 *
 *
 */
@Service
@Slf4j
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements ITUserService, UserDetailsService {


    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        log.info("用户进行认证的用户名为：{}", s);
        TUser tUser = new TUser();
        tUser.setUserName(s);
        TUser tUser1 = this.baseMapper.selectOne(tUser);
        if (tUser1 == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        return new User(s, tUser1.getPassword(), AuthorityUtils.createAuthorityList("admin"));
    }
}
